package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.UserService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/saveUser", name = "保存用户")
    public ResponseEntity save(@RequestBody User user){
        return userService.save(user);

    }

    @GetMapping(value = "/findByPhone", name = "查询用户")
    public ResponseEntity findByPhone(String phone){
        return userService.findByPhone(phone);
    }


    @PostMapping(value = "/login", name = "发送验证码")
    public ResponseEntity login(@RequestBody Map param) throws ClientException {
        String phone =(String)param.get("phone");
        return userService.login(phone);
    }


    @PostMapping(value = "/loginVerification", name = "注册/登录")
    public ResponseEntity loginVerification(@RequestBody Map param) throws ClientException {
        String phone =(String)param.get("phone");
        String yzm =(String)param.get("verificationCode");
        return  userService.loginVerification(phone,yzm);
    }

    @PostMapping(value = "/loginReginfo", name = "用户个人信息完善")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token) throws ClientException {
       return userService.loginReginfo(userInfo,token);
    }

    @PostMapping(value = "/loginReginfo/head", name = "人脸识别/图片上传/修改个人信息")
    public ResponseEntity loginReginfoimg(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws ClientException, IOException {
        return userService.loginReginfoimg(headPhoto,token);
    }

}
